﻿using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;

namespace PublicClass
{
    public class Settings
    {
        public event EventHandler<SettingsUpdateEventArgs> UpdateSettingCompleted;

        protected virtual void OnUpdateSettingCompleted(SettingsUpdateEventArgs e)
        {
            EventHandler<SettingsUpdateEventArgs> handler = UpdateSettingCompleted;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        private bool _AreaView=false;

        public bool AreaView
        {
            get { return _AreaView; }
            set 
            {
                _AreaView = value;
                SettingsUpdateEventArgs e = new SettingsUpdateEventArgs();
                e.Result = this;
                OnUpdateSettingCompleted(e);
            }
        }

        private bool _ViewLocation = true;

        public bool ViewLocation
        {
            get { return _ViewLocation; }
            set
            {
                _ViewLocation = value;
                SettingsUpdateEventArgs e = new SettingsUpdateEventArgs();
                e.Result = this;
                OnUpdateSettingCompleted(e);
            }
        }

        private bool _RadaSearch = true;

        public bool RadaSearch
        {
            get { return _RadaSearch; }
            set
            {
                _RadaSearch = value;
                SettingsUpdateEventArgs e = new SettingsUpdateEventArgs();
                e.Result = this;
                OnUpdateSettingCompleted(e);
            }
        }

        private double _RadaZoom=4.7;

        public double RadaZoom
        {
            get { return _RadaZoom; }
            set {
                _RadaZoom = value;
                SettingsUpdateEventArgs e = new SettingsUpdateEventArgs();
                e.Result = this;
                OnUpdateSettingCompleted(e);
            }
        }

        public void SaveSettings()
        {
            XmlWriterSettings xmlWriterSetting = new XmlWriterSettings();
            xmlWriterSetting.Indent = true;
            // Lấy thông tin IsolateStorage
            using (IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // Tạo một FileStream để tạo file hay mở file
                using (IsolatedStorageFileStream Stream = new IsolatedStorageFileStream("Settings.xml", System.IO.FileMode.Create, isoStorage))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    using (XmlWriter xmlWriter = XmlWriter.Create(Stream, xmlWriterSetting))
                    {
                        // Viết dữ liệu theo Serialize
                        serializer.Serialize(xmlWriter, this);
                    }
                }
            }
        }

        public Settings LoadSettings()
        {
            Settings Result = new Settings();

            using (IsolatedStorageFile File = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (File.FileExists("Settings.xml"))
                {
                    using (IsolatedStorageFileStream FileStream = new IsolatedStorageFileStream("Settings.xml", System.IO.FileMode.Open, File))
                    {
                        try
                        {
                            // Trong FileStream thực hiện Deserialize
                            XmlSerializer serialize = new XmlSerializer(typeof(Settings));
                            // Ép kiểu về đúng kiểu ban đầu và Gán lại cho Source 
                            Result = (Settings)serialize.Deserialize(FileStream);
                        }
                        catch (Exception ex)
                        {
                            FileStream.Close();

                            File.DeleteFile("Settings.xml");
                        }
                    };
                };
                SettingsUpdateEventArgs e = new SettingsUpdateEventArgs();

                e.Result = Result;

                OnUpdateSettingCompleted(e);

                return Result;
            }
        }
    }

    public class SettingsUpdateEventArgs : EventArgs
    {
        public Settings Result;
    }
}
